myApp.controller('usersController',function($scope,$state,toaster,$uibModal,$log,$serUsers,$filter,$localStorage) {
    var ctrl = this;
    ctrl.vm = {
        data:{},
        search:{

        },
        searchFn:function(){
            vm.init();
        },
        reset:function(){
            ctrl.vm.search.id='';
            ctrl.vm.search.startTime='';
            ctrl.vm.search.endTime='';
            ctrl.vm.search.telid='';
            ctrl.vm.search.emailid='';
            ctrl.vm.search.phone='';
            ctrl.vm.search.state='';

            vm.init();
        },
        qiyong:function(id){
            $serUsers.enable({
                id:id
            }).then(function(res){
                if(res.code==1){
                    toaster.success("提示",res.message);
                    vm.init();
                }else{
                    toaster.info("提示",res.message);
                }
            })
        },
        jinyomg:function (id) {
            $serUsers.disable({
                id:id
            }).then(function(res){
                if(res.code==1){
                    toaster.success("提示",res.message);
                    vm.init();
                }else{
                    toaster.info("提示",res.message);
                }
            })
        }
    };
    $scope.vm={
        page:{}
    };
    /*跳转页面*/
    $scope.pageChanged = function() {
        vm.init($scope.vm.page.current_page)
    };
    /*每页显示数量显示*/
    $scope.placement = {
        options: [5,10,20,50],
        pageSize:5,
        selected: 20,
        changeFn:function(){
            vm.init(0)
        }
    };
    var vm = {
        init:function(page){
            var start = $filter('date')(ctrl.vm.search.startTime,'yyyy-MM-dd 00:00:01');
            var end = $filter('date')(ctrl.vm.search.endTime,'yyyy-MM-dd 11:59:59');
            $serUsers.getList({
                page:page||1,
                size:$scope.placement.selected,
                id:  ctrl.vm.search.id||'',
                status:  ctrl.vm.search.state||'',
                phone:  ctrl.vm.search.telid||'',
                email:  ctrl.vm.search.emailid||'',
                created_start:start||'',
                created_end:end||''
            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.data = res.data;
                    $scope.vm.page = res.addtion_data;
                }else{
                    ctrl.vm.data='';
                    toaster.info('提示',res.message);
                }
            })
        }
    };

    $scope.model = {
        info: {

        },
        open: function (data) {
            $scope.model.info = data;
            var modalInstance = $uibModal.open({
                templateUrl: 'addUser.html',
                controller: 'addUserCtrl',
                windowClass: 'adminOpen',
                resolve: {
                    info: function () {
                        return $scope.model.info;
                    }
                }
            });
            modalInstance.result.then(function (res) {
                if (res) {
                    vm.init();
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        }
    };
    vm.init();
    $scope.open = function() {
        $scope.popup.opened = true;
    };
    $scope.popup = {
        opened: false
    };
    $scope.open1 = function() {
        $scope.popup1.opened = true;
    };
    $scope.popup1 = {
        opened: false
    };
});

myApp.controller('addUserCtrl', ['$scope', '$uibModalInstance', 'info', '$serUsers','toaster', function($scope, $uibModalInstance, info,$serUsers, toaster) {
    $scope.password ='';
    $scope.ok = function () {
        $serUsers.resetPassword({
            id:info,
            password:$scope.password
        }).then(function(res){
            if(res.code==1){
                toaster.success('提示',res.message);
                $uibModalInstance.close($scope.info);
            }else{
                toaster.info('提示',res.message);
            }
        });

    };

    $scope.cancel = function () {
        $uibModalInstance.dismiss(0);
    };
}]);